# Contributor: Timo Teras <timo.teras@iki.fi>
# Contributor: Jakub Jirutka <jakub@jirutka.cz>
# Maintainer: Timo Teras <timo.teras@iki.fi>
pkgname=openjdk7
_icedteaver=2.6.28
_icedteaversrc=$_icedteaver
# pkgver is <JDK version>.<JDK update>
# check icedtea JDK when updating
pkgver=7.321.$_icedteaver
pkgrel=0
pkgdesc="OpenJDK 7 via IcedTea"
provider_priority=7
url="https://icedtea.classpath.org/"
# riscv64 blocked by java-gcj-compat
arch="all !riscv64"
license="GPL-2.0 with Classpath"
depends="$pkgname-jre java-cacerts"
options="sover-namecheck"
makedepends="bash findutils tar zip file gawk util-linux libxslt
	autoconf automake linux-headers coreutils ca-certificates
	nss-dev cups-dev jpeg-dev giflib-dev libpng-dev libxt-dev
	lcms2-dev libxp-dev libxtst-dev libxinerama-dev zlib-dev
	libxrender-dev alsa-lib-dev freetype-dev fontconfig-dev
	gtk+2.0-dev krb5-dev pcsc-lite-dev lksctp-tools-dev
	libxcomposite-dev nss-static xz"

# upstream binary versions
_rhino_ver=1.7.7.2 # rhino 1.7.8 is java 8+ only
_ant_ver=1.9.16

case $CARCH in
x86)		_jarch=i386;;
x86_64)		_jarch=amd64;;
aarch64)	_jarch=aarch64;;
arm*)		_jarch=arm
		# openjdk has quite a bit of arm assembly producing textrels
		# and it's non trivial to fix. allow it for now.
		options="$options textrels";;
s390x)		_jarch=s390x;;
ppc64le)	_jarch=ppc64le;;
esac

_install_base=/usr/lib/jvm/java-1.7-openjdk
_jrelib="$_install_base/jre/lib/$_jarch"

# exclude xawt from ldpath to avoid duplicate provides for libmawt.so
# (also in headless). in future this should be a virtual provides.
ldpath="$_jrelib:$_jrelib/native_threads:$_jrelib/headless:$_jrelib/server:$_jrelib/jli"
sonameprefix="$pkgname:"

subpackages="$pkgname-jre-lib:jrelib:noarch $pkgname-jre $pkgname-jre-base:jrebase
	$pkgname-doc:doc"

if [ "$BOOTSTRAP" != "no" ]; then
	makedepends="$makedepends java-gcj-compat"
	BOOTSTRAP_JAVA_HOME=/usr/lib/jvm/java-1.5-gcj/
else
	makedepends="$makedepends $pkgname"
	BOOTSTRAP_JAVA_HOME="$_install_base"
fi

_dropsver=$_icedteaver
_dropsurl=https://icedtea.classpath.org/download/drops/icedtea7/$_dropsver

source="https://icedtea.classpath.org/download/source/icedtea-$_icedteaversrc.tar.xz
	openjdk-$_dropsver.tar.bz2::$_dropsurl/openjdk.tar.bz2
	corba-$_dropsver.tar.bz2::$_dropsurl/corba.tar.bz2
	jaxp-$_dropsver.tar.bz2::$_dropsurl/jaxp.tar.bz2
	jaxws-$_dropsver.tar.bz2::$_dropsurl/jaxws.tar.bz2
	jdk-$_dropsver.tar.bz2::$_dropsurl/jdk.tar.bz2
	langtools-$_dropsver.tar.bz2::$_dropsurl/langtools.tar.bz2
	hotspot-$_dropsver.tar.bz2::$_dropsurl/hotspot.tar.bz2
	https://archive.apache.org/dist/ant/binaries/apache-ant-$_ant_ver-bin.tar.gz
	https://github.com/mozilla/rhino/releases/download/Rhino${_rhino_ver//./_}_Release/rhino-$_rhino_ver.zip

	icedtea-hotspot-fix-nullcheck.patch
	icedtea-hotspot-musl.patch
	icedtea-hotspot-musl-ppc.patch
	icedtea-hotspot-noagent-musl.patch
	icedtea-hotspot-uclibc-fixes.patch
	icedtea-jdk-execinfo.patch
	icedtea-jdk-fix-build.patch
	icedtea-jdk-fix-compile-optional.patch
	icedtea-jdk-fix-gcc10.patch
	icedtea-jdk-fix-ipv6-init.patch
	icedtea-jdk-musl.patch
	icedtea-jdk-no-soname.patch
	icedtea-jdk-xattr.patch
	icedtea-cpio.patch
	icedtea-jdk-disable-vfork.patch

	fix-xattr-include.patch
	autoconf-2.7x.patch
	"

# secfixes:
#   7.321.2.6.28-r0:
#     - CVE-2021-35550
#     - CVE-2021-35556
#     - CVE-2021-35559
#     - CVE-2021-35561
#     - CVE-2021-35564
#     - CVE-2021-35565
#     - CVE-2021-35586
#     - CVE-2021-35588
#     - CVE-2021-35603
#   7.311.2.6.27-r0:
#     - CVE-2021-2341
#     - CVE-2021-2369
#     - CVE-2021-2432
#   7.301.2.6.26-r0:
#     - CVE-2021-2161
#     - CVE-2021-2163
#   7.281.2.6.24-r0:
#     - CVE-2020-14803
#   7.271.2.6.23-r0:
#     - CVE-2019-2745
#     - CVE-2020-14779
#     - CVE-2020-14781
#     - CVE-2020-14782
#     - CVE-2020-14792
#     - CVE-2020-14796
#     - CVE-2020-14797
#     - CVE-2020-14798
#   7.261.2.6.23-r0:
#     - CVE-2020-14577
#     - CVE-2020-14578
#     - CVE-2020-14579
#     - CVE-2020-14581
#     - CVE-2020-14583
#     - CVE-2020-14593
#     - CVE-2020-14621
#   7.261.2.6.22-r0:
#     - CVE-2020-2756
#     - CVE-2020-2757
#     - CVE-2020-2773
#     - CVE-2020-2781
#     - CVE-2020-2800
#     - CVE-2020-2803
#     - CVE-2020-2805
#     - CVE-2020-2830
#   7.251.2.6.21-r0:
#     - CVE-2020-2583
#     - CVE-2020-2590
#     - CVE-2020-2593
#     - CVE-2020-2601
#     - CVE-2020-2604
#     - CVE-2020-2654
#     - CVE-2020-2659
#   7.241.2.6.20-r0:
#     - CVE-2019-2894
#     - CVE-2019-2933
#     - CVE-2019-2945
#     - CVE-2019-2949
#     - CVE-2019-2958
#     - CVE-2019-2962
#     - CVE-2019-2964
#     - CVE-2019-2973
#     - CVE-2019-2978
#     - CVE-2019-2981
#     - CVE-2019-2983
#     - CVE-2019-2987
#     - CVE-2019-2988
#     - CVE-2019-2989
#     - CVE-2019-2992
#     - CVE-2019-2999
#   7.231.2.6.19-r0:
#     - CVE-2019-2766
#     - CVE-2019-2769
#     - CVE-2019-2786
#     - CVE-2019-2816
#     - CVE-2019-2842
#   7.221.2.6.18-r0:
#     - CVE-2019-2602
#     - CVE-2019-2684
#     - CVE-2019-2698
#   7.211.2.6.17-r0:
#     - CVE-2018-11212
#     - CVE-2019-2422
#     - CVE-2019-2426
#   7.201.2.6.16-r0:
#     - CVE-2018-3136
#     - CVE-2018-3139
#     - CVE-2018-3149
#     - CVE-2018-3169
#     - CVE-2018-3180
#     - CVE-2018-3214
#     - CVE-2018-13785

builddir="$srcdir/icedtea-$_icedteaver"

unpack() {
	if [ -z "$force" ]; then
		verify
		initdcheck
	fi
	mkdir -p "$srcdir"
	msg "Unpacking sources..."
	unxz -c icedtea-$_icedteaversrc.tar.xz | tar -C "$srcdir" -x
	tar -C "$srcdir" -xf apache-ant-$_ant_ver-bin.tar.gz
	unzip -o -q "rhino-$_rhino_ver.zip" -d "$srcdir"
}

prepare() {
	local ver_u=$(sed -En 's/^\s*JDK_UPDATE_VERSION\s*=\s*(\S+).*/\1/p' acinclude.m4)
	local ver_b=$(sed -En 's/^\s*BUILD_VERSION\s*=\s*b(\S+).*/\1/p' acinclude.m4)
	[ "${pkgver%.$_icedteaver}" = "7.$ver_u" ] \
		|| die "Version mismatch, source is 7.$ver_u.$_icedteaver, but abuild defines $pkgver!"

	# Busybox sha256 does not support longopts
	sed -e "s/--check/-c/g" -i Makefile.am

	for patch in $source; do
		case $patch in
		icedtea-*.patch)
			cp ../$patch patches
			;;
		*.patch)
			msg "Applying patch $patch"
			patch -p1 -i "$srcdir"/$patch
			;;
		esac
	done

	./autogen.sh
}

build() {
	[ -z "$JOBS" ] && export JOBS=$(echo $MAKEFLAGS | sed -n -e 's/.*-j\([0-9]\+\).*/\1/p')
	export ALT_PARALLEL_COMPILE_JOBS="${JOBS:-2}"
	export HOTSPOT_BUILD_JOBS="${JOBS:-2}"

	export JAVA_HOME=$BOOTSTRAP_JAVA_HOME
	export PATH=$JAVA_HOME/bin:$srcdir/apache-ant-$_ant_ver/bin:$PATH
	export DISTRIBUTION_PATCHES=""

	# Explicitly set the C++ standard as the default has changed on GCC 6+
	# and disable optimizations that lead to a broken JVM. These options
	# has been adopted from the Fedora package.
	export EXTRA_CPP_FLAGS="$CXXFLAGS -std=gnu++98 -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-strict-overflow"
	# CXXFLAGS doesn't make it to all calls, so we set the C++ standard
	# version for C too.
	export EXTRA_CFLAGS="$CFLAGS -std=gnu++98 -Wno-error -fno-delete-null-pointer-checks -fno-lifetime-dse -fno-strict-overflow"

	local patch
	for patch in $source; do
		case $patch in
		icedtea-*.patch)
			export DISTRIBUTION_PATCHES="$DISTRIBUTION_PATCHES patches/$patch"
			;;
		esac
	done
	echo "icedtea patches: $DISTRIBUTION_PATCHES"

	# --disable-arm32-jit: https://icedtea.classpath.org/bugzilla/show_bug.cgi?id=2942
	./configure \
		--build=$CBUILD \
		--host=$CHOST \
		--prefix=$_install_base \
		--sysconfdir=/etc \
		--mandir=/usr/share/man \
		--infodir=/usr/share/info \
		--localstatedir=/var \
		--with-parallel-jobs=${JOBS:-2} \
		--disable-dependency-tracking \
		--disable-downloading \
		--disable-arm32-jit \
		--disable-docs \
		--with-rhino=$srcdir/rhino$_rhino_ver/lib/rhino-$_rhino_ver.jar \
		--with-openjdk-src-zip=$srcdir/openjdk-$_dropsver.tar.bz2 \
		--with-hotspot-src-zip=$srcdir/hotspot-$_dropsver.tar.bz2 \
		--with-corba-src-zip=$srcdir/corba-$_dropsver.tar.bz2 \
		--with-jaxp-src-zip=$srcdir/jaxp-$_dropsver.tar.bz2 \
		--with-jaxws-src-zip=$srcdir/jaxws-$_dropsver.tar.bz2 \
		--with-jdk-src-zip=$srcdir/jdk-$_dropsver.tar.bz2 \
		--with-langtools-src-zip=$srcdir/langtools-$_dropsver.tar.bz2 \
		--with-jdk-home=$BOOTSTRAP_JAVA_HOME \
		--with-pkgversion="Alpine $pkgver-r$pkgrel"

	msg "Icedtea boot"
	make icedtea-boot SHELL=/bin/bash
	msg "Icedtea boot done"
	make
}

package() {
	provides=java-jdk
	mkdir -p "$pkgdir"/$_install_base
	cp -a "$builddir"/openjdk.build/j2sdk-image/* "$pkgdir"/$_install_base
	rm "$pkgdir"/$_install_base/src.zip

	# symlink to shared java cacerts store
	rm -f "$pkgdir"/$_install_base/jre/lib/security/cacerts
	ln -sf /etc/ssl/certs/java/cacerts \
		"$pkgdir"/$_install_base/jre/lib/security/cacerts
}

jrelib() {
	pkgdesc="OpenJDK 7 Java Runtime (class libraries)"
	depends=""

	local a; for a in jre/lib/zi jre/lib/images jre/lib/*.jar \
			jre/lib/security jre/lib/ext/*.jar jre/lib/cmm \
			jre/ASSEMBLY_EXCEPTION jre/THIRD_PARTY_README jre/LICENSE; do

		local dirname=${a%/*}
		mkdir -p "$subpkgdir"/$_install_base/$dirname
		mv "$pkgdir"/$_install_base/$a "$subpkgdir"/$_install_base/$dirname
	done
}

jrebase() {
	pkgdesc="OpenJDK 7 Java Runtime (no GUI support)"
	depends="$pkgname-jre-lib java-common java-cacerts"
	provides=java-jre-headless

	mkdir -p "$subpkgdir"/$_install_base/bin

	local a; for a in java orbd rmid servertool unpack200 keytool \
			pack200 rmiregistry tnameserv; do
		mv "$pkgdir"/$_install_base/bin/$a "$subpkgdir"/$_install_base/bin
	done

	# rest of the jre subdir (which were not taken by -jre subpkg)
	mv "$pkgdir"/$_install_base/jre "$subpkgdir"/$_install_base
}

jre() {
	pkgdesc="OpenJDK 7 Java Runtime"
	# manually depend to avoid clash with libgcj's libjvm.so
	depends="so:openjdk7:libjvm.so=0"
	provides=java-jre

	mkdir -p "$subpkgdir"
	local a; for a in jre/bin/policytool \
			bin/appletviewer \
			bin/policytool \
			jre/lib/$_jarch/xawt \
			jre/lib/$_jarch/libsplashscreen.so; do

		local dirname=${a%/*}
		mkdir -p "$subpkgdir"/$_install_base/$dirname
		mv "$pkgdir"/$_install_base/$a "$subpkgdir"/$_install_base/$dirname
	done
}

doc() {
	default_doc
	mkdir -p "$subpkgdir"/$_install_base/
	mv "$pkgdir"/$_install_base/man "$subpkgdir"/$_install_base/
}

sha512sums="
00a3c4e69d50c365653d1afb9f344809e74802fad91cf153471de409677396ea6d1f4e68eeec45bb0a9fc368b6a3108047dc57960e2a975158c66233575092a7  icedtea-2.6.28.tar.xz
9c57d67bd55f5baba97a4f000ec5e23d8511682efa08a5c4780bb09acf9363ca640bdb187e60bf5dbc564b04e4b9caa8a5e928559052601557ce390e7056fd0e  openjdk-2.6.28.tar.bz2
9719f09aace720af6fb2cc3266b07058b982c57ae157fe9210e82028884a58519ba6a67e63fcf9031d943999b14c6066d2076944fc458cf66fd8ad5f14ab9876  corba-2.6.28.tar.bz2
5392528f5ce2c8a48724b184cda9d50f6a9feecec83a4d3568e64a35c24396fe09ca5d3716cacd6bcb9e6ea00427f1ef392f7da16e9a18e098a8bb4fb484d914  jaxp-2.6.28.tar.bz2
56da89c34120295c39f6d8a672be6214059d7b741407ffa102a7b0519a19b04302d1b332d97c577e4664fcd6217f673a53008e6ada89336092d6afe6e983b3c0  jaxws-2.6.28.tar.bz2
9bc079bf3e28ea211b4bf4f738507ecb5610e18e27ff4af7e8bdfea3c2d219daa4a001f1e2d9ca71a7e7386b9ce40d5f16ff985b70624761a375219b8d699f5b  jdk-2.6.28.tar.bz2
6aa22ee0950801e710e47ef00ae7fb086bbef406d1d95c31bd8b16ff9254641673636fdf2a4922afd5ea0b9d2f23d8ed1ec6c68b6eb4055e95a68802fe1c983c  langtools-2.6.28.tar.bz2
da0fd6a9055ed82adffc89f955b242ec2fb9927d59f02934fbcc885ea787f81661dc235e52e859fb872639def099a06ff7c7b1a1a5fc16417fd5a654f3bfdc23  hotspot-2.6.28.tar.bz2
8d542a7a636a491e76170148881b6f413f4564aad1ab034f426bd946be93382001862bd6c60865aa2b57b39b9633e0181fe8997dd6323123aaf76b410ec4a366  apache-ant-1.9.16-bin.tar.gz
1b9e8721749e81c5420a00af1e00ee0e4f48624ccb4e9aa969032114116ad50f59b254d4d16d74feff74de64157cc8b0a2ead9b555907c84b7055b796fba9a75  rhino-1.7.7.2.zip
7d2133031dbcebf36d14231f87fa32f34efb35c6100c72429b4f989aa278bd72bb852f7aca74b83db06477979b5a4f0f5bfd2cbd5d3c276b59401914a90261bf  icedtea-hotspot-fix-nullcheck.patch
74e38e41db0dcca6ec91d3a83330f06904342ba8e950d896c4544db72b76a3ae6665793b83d7bbeb5a0cd3896b48707c8beda92ef0eb0e7f821c15e591e2fd49  icedtea-hotspot-musl.patch
e5cf4d70f96fc1e72ae8b97a887adb96092ff36584711cbb8de9d9fa9e859cb8731d638838de0d9591239fc44ffe5c74422d1842bd9f10a0c00dff1627bdeeef  icedtea-hotspot-musl-ppc.patch
ae4e32af42dfed963f5ee6dbc65d102e2ed51778ada829d8ae230904cd9e0473dacf7362a36b3e83929a9c1c7d176abc8e409bfbe27214ebb9f0ae1410dee282  icedtea-hotspot-noagent-musl.patch
822eee0dc4d3ba677a289dfeb3668b536d2d626708390a9d9702fb4144a481fd443a215a0d2041c6026113837aafa4ba0b5e9ead8589d2da6717a238bbc95a5a  icedtea-hotspot-uclibc-fixes.patch
0391970e6a32946aa3cccf38fdef9c0fe2af26cd0df824b98aa2fcfa1bf661d4a68e339bffcfd16f386c565fc68bb28a29208a67d4bad8a0e847ad02bd8becbb  icedtea-jdk-execinfo.patch
8fadeee6ea9886c7ee3118a1abaee2fbd04931a3ba880062bc97397ad30aab114a83542c888461a5a8a1d131c4e73920872317c96620e2a8c4689620adf9e9c3  icedtea-jdk-fix-build.patch
38114b1de864539dd5ba35add8bfe0de8b85d6ebcb4d05e82cebc318ca27883737739315471855cfe0fd0cbc0a26d916aeb2304a23a75abb6a351aa51957e8a9  icedtea-jdk-fix-compile-optional.patch
9b4ad4a593672edd617c514d3d9158324762d7d91049eca0b3c7f307c668cf20c1db94ecd935ab35d149488524d23db5dd77d7d45572f2fa1a7643c26ae4a32c  icedtea-jdk-fix-gcc10.patch
48533f87fc2cf29d26b259be0df51087d2fe5b252e72d00c6ea2f4add7b0fb113141718c116279c5905e03f64a1118082e719393786811367cf4d472b5d36774  icedtea-jdk-fix-ipv6-init.patch
44a35941c80f408d0607e32763b3b6ccee21e1d39886309327d3d74d2900117e4346ef59e77c663fd022fec10ee8f365eeb46c1260014d5765d226ce175ce3c5  icedtea-jdk-musl.patch
bf4b184e170f7b0ff64ab30d2162784fe2bd5460d1fa31973259f7065fd4c511c46f97724fe2bd72bb94e9006cb568d0e0c87d1a9c90819e65880f8f44830bb1  icedtea-jdk-no-soname.patch
e703e5f68e63f130e758b3fa813872363090c08eee6a6366f4784cf6a191a90a3642ec71e016bd05e48a66ad0ae56db8b0e318b101aca5d0885af8705423b770  icedtea-jdk-xattr.patch
a54c79c82afa1bc95265397b274260584c8b8c6be1651ddfb907d9523a809ea4581409e0d3fb0bbb63ef5a204e8ce29b7940e78cd640af1f490ae938c59129b6  icedtea-cpio.patch
c70fee8f1a10c155494ab9c6e55e979d8a3e23f12358ffe9de2029ae15feff361957f1aa43af43c90ecf00523f92e3333b40311a8d0c132ebd456486cb85ed8a  icedtea-jdk-disable-vfork.patch
4c874f0e9f3f54f688a1faf64fa3734e31f139f465ce77d12aac87558764ef66055de6cfac5b60efef2833c64256136c70f3cf5dc2712de62fc5dbebe3a8fbcd  fix-xattr-include.patch
d05ef2da69d3e02cbee5bd55be86b61c0a701f65e8cfac287f9b5025934059870d09d5b006c1ffe4e9b9c8aca33842199512d3433249e19346d855e768c0d94f  autoconf-2.7x.patch
"
